package com.jihu.mall.product.config;

import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @author jihu
 * @date 2021/4/10
 */
@Configuration
//@ConfigurationProperties(prefix = "gulimall.thread")
public class ThreadConfig {

    //核心线程数
    private static final Integer corePoolSize = 20;
    //最大线程数
    private static final Integer maximumPoolSize = 200;
    //核心线程数存活时间
    private static final Long keepAliveTime = 10L;
    //核心线程数存活时间单位
    private static final TimeUnit unit = TimeUnit.SECONDS;
    //阻塞队列
    private static final BlockingQueue<Runnable> workQueue= new LinkedBlockingQueue<>(100000);
    //线程创建工厂
    private static final ThreadFactory threadFactory = Executors.defaultThreadFactory();
    //拒绝策略
    private static final RejectedExecutionHandler handler = new ThreadPoolExecutor.AbortPolicy();

    @Bean("threadPoolExecutor")
    public ThreadPoolExecutor threadPoolExecutor(){
        ThreadPoolExecutor executor =
                new ThreadPoolExecutor(
                        corePoolSize,
                        maximumPoolSize,
                        keepAliveTime,
                        unit,workQueue,
                        threadFactory,
                        handler);
        return executor;
    }

}
